package com.mvc.controller;

import com.mvc.entity.Type;
import com.mvc.model.JsonUtil;
import com.mvc.model.Result;
import com.mvc.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ProjectName: dior
 * @Package: com.mvc.controller
 * @ClassName: TypeController
 * @Description: TODO
 * @Author: Bang
 * @Date: 2021/11/27 12:08
 * @Version: 1.0
 **/
@Controller
@RequestMapping(value = "/type", produces = "text/html;charset=UTF-8")
public class TypeController {
    @Resource
    TypeService typeService;

    @RequestMapping(value = "/query.do")
    @ResponseBody
    public String query(){

        List<Type> typeList = typeService.query();
        Result result = new Result();
        result.setCode(0);
        result.setData(typeList);
        return JsonUtil.stringify(result);
    }

    @RequestMapping(value = "/add.do")
    @ResponseBody
    public String add(@RequestBody Type type){
        Integer n = typeService.add(type);
        Result result = new Result();
        if(n == 1) {
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1000);
            result.setErrorMsg("失败");
        }
        return JsonUtil.stringify(result);
    }
}
